/*
 * FormLogIn.java
 *
 * Created on 12 Maret 2008, 19:26
 */

package edu.unindra.elearning.form.addin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import edu.unindra.elearning.util.PanelTranparan;
import edu.unindra.elearning.util.ViewPortGlass;
import usu.widget.ButtonGlass;
import java.awt.Font;

/**
 * 
 * @author usu
 */
public class ElearningFormSubMainMateriSoal extends JPanel implements
		ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 815072320793043258L;
	private PanelTranparan panelEfect;
	private JScrollPane jsp;

	private ViewPortGlass viewPortGlass;
	private JTextArea textSoal;

	private ButtonGroup bg;
	private JRadioButton rd1;
	private JRadioButton rd2;
	private JRadioButton rd3;
	private JRadioButton rd4;

	private ButtonGlass btnNext;
	private ButtonGlass btnFinish;

	private String kls;
	private String mtr;
	private String soal = "";

	private double score;

	private int countTrue;
	private int idx;
	private int countSoal;
	private int b;

	private String[] answer;
	private String[][] question;

	private HashMap<Integer, String> map;

	/** Creates new form ElearningFormSubMainMateriSoal */
	public ElearningFormSubMainMateriSoal() {
		btnFinish = new ButtonGlass();

		initComponents();
	}

	public ButtonGlass getBtnFinish() {
		return btnFinish;
	}

	public ButtonGlass getBtnNext() {
		return btnNext;
	}

	public void setBtnNext(ButtonGlass btnNext) {
		this.btnNext = btnNext;
	}

	public void initComponents() {
		idx = 0;
		map = new HashMap<Integer, String>();

		setOpaque(false);
		setLayout(new java.awt.GridBagLayout());

		panelEfect = new PanelTranparan();
		panelEfect.setToolTipText("E'Learning");
		jsp = new JScrollPane();

		viewPortGlass = new ViewPortGlass();
		textSoal = new JTextArea();

		viewPortGlass.setView(textSoal);
		textSoal.setColumns(10);
		textSoal.setFont(new Font("Tahoma", Font.BOLD, 18));
		textSoal.setForeground(new java.awt.Color(255, 255, 255));
		textSoal.setRows(7);
		textSoal.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
				10, 10));
		textSoal.setOpaque(false);

		bg = new ButtonGroup();
		rd1 = new JRadioButton();
		rd1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		rd1.setOpaque(false);

		rd2 = new JRadioButton();
		rd2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		rd2.setOpaque(false);

		rd3 = new JRadioButton();
		rd3.setFont(new Font("Tahoma", Font.PLAIN, 14));
		rd3.setOpaque(false);

		rd4 = new JRadioButton();
		rd4.setFont(new Font("Tahoma", Font.PLAIN, 14));
		rd4.setOpaque(false);

		bg.add(rd1);
		bg.add(rd2);
		bg.add(rd3);
		bg.add(rd4);

		btnNext = new ButtonGlass();

		btnNext.setText("Selanjutnya");
		btnNext.setToolTipText("Klik ini untuk kembali ke soal selanjutnya");
		btnNext.setRoundRect(true);
		btnNext.addActionListener(this);

		btnFinish.setText("Selesai");
		btnFinish.setToolTipText("Klik ini untuk melihat hasil");
		btnFinish.setRoundRect(true);
		btnFinish.addActionListener(this);

		jsp.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		jsp.setOpaque(false);
		jsp.setViewport(viewPortGlass);

		panelEfect.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(0, 0, 0)));
		panelEfect.setRound(false);

		javax.swing.GroupLayout panelEfectLayout = new javax.swing.GroupLayout(
				panelEfect);
		panelEfect.setLayout(panelEfectLayout);
		panelEfectLayout
				.setHorizontalGroup(panelEfectLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								panelEfectLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelEfectLayout
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addComponent(
																rd1,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																rd2,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																rd3,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																rd4,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))));

		panelEfectLayout
				.setVerticalGroup(panelEfectLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								panelEfectLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelEfectLayout
														.createParallelGroup(Alignment.BASELINE))
										.addComponent(rd1,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(rd2,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(rd3,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(rd4,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);

		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)
												.addComponent(
														panelEfect,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jsp,
														GroupLayout.Alignment.TRAILING,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		btnNext,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		452,
																		Short.MAX_VALUE)
																.addComponent(
																		btnFinish,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addContainerGap()));

		layout.setVerticalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(jsp, GroupLayout.DEFAULT_SIZE,
										GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(panelEfect,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE, 140)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														btnNext,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														btnFinish,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));

		readqa(idx);
	}

	public void readqa(int idx) {
		if (idx == 0) {
			textSoal.setText("");
			rd1.setText("");
			rd2.setText("");
			rd3.setText("");
			rd4.setText("");
		} else {
			soal = question[idx - 1][0];

			if (soal.contains("#")) {
				StringBuffer sb = new StringBuffer();
				String[] split = soal.split("#");
				for (int i = 0; i < split.length; i++) {
					sb.append(split[i] + "\r\n");
				}
				soal = sb.toString();
			}
			textSoal.setText(soal);
			rd1.setText(question[idx - 1][1]);
			rd2.setText(question[idx - 1][2]);
			rd3.setText(question[idx - 1][3]);
			rd4.setText(question[idx - 1][4]);
		}

	}

	public void initializedata(String path) throws IOException {
		btnFinish.setEnabled(false);

		if (path.length() > 0) {
			loadData(path);
			readqa(1);
		}

	}

	private void loadData(String f) throws IOException {
		FileInputStream inputWorkbook = new FileInputStream(new File(f));
		try {
			Workbook w = Workbook.getWorkbook(inputWorkbook);
			Sheet sheet = w.getSheet(0);

			countSoal = sheet.getRows() - 9;
			b = sheet.getColumns() - 1;
			question = new String[countSoal][b];
			answer = new String[countSoal];

			for (int i = 9; i < sheet.getRows(); i++) {
				for (int j = 1; j < sheet.getColumns(); j++) {
					Cell cell = sheet.getCell(j, i);
					if (j == 6) {
						answer[i - 9] = cell.getContents();
					} else {
						question[i - 9][j - 1] = cell.getContents();
					}
				}
			}

		} catch (BiffException e) {
			e.printStackTrace();
		}

		inputWorkbook.close();

	}

	public String getSelection() {
		String selectedChoice = null;

		Enumeration<AbstractButton> buttons = bg.getElements();
		while (buttons.hasMoreElements()) {
			JRadioButton temp = (JRadioButton) buttons.nextElement();
			if (temp.isSelected()) {
				selectedChoice = temp.getText();
			}
		}
		return (selectedChoice);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equalsIgnoreCase("Selanjutnya")) {
			if (idx < (countSoal - 1)) {
				map.put(idx, getSelection());
				idx++;
				readqa(idx + 1);

			} else {
				countTrue = 0;
				score = 0.0;

				map.put(idx, getSelection());
				for (int i = 0; i < countSoal; i++) {
					if (answer[i].equals(map.get(i))) {
						countTrue++;
					}
				}
				score = (Double.valueOf(countTrue) / Double.valueOf(countSoal)) * 100;

				btnFinish.setEnabled(true);
				btnNext.setEnabled(false);
				idx = 0;
			}
		}

	}

	public void setKls(String kls) {
		this.kls = kls;
	}

	public void setMtr(String mtr) {
		this.mtr = mtr;
	}

	public double getScore() {
		return score;
	}

	public int getCountTrue() {
		return countTrue;
	}

	public int getCountSoal() {
		return countSoal;
	}

	public String getKls() {
		return kls;
	}

	public String getMtr() {
		return mtr;
	}

}
